<template>
  <div>
    <el-dialog
      :title="dialogFormTitle"
      :fullscreen="fullscreen"
      :visible.sync="dialogFormVisible"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="true"
    >
      <el-form :model="operForm" ref="operForm" :rules="rules" label-width="80px">
        <el-form-item label="工号" prop="teaid">
          <el-input
            v-model.trim="operForm.teaid"
            maxlength="20"
            autocomplete="off"
            :disabled="this.$parent.dialogId !== 0"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="passwd">
          <el-input type="password" v-model="operForm.passwd" maxlength="16" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="passwd2">
          <el-input
            type="password"
            maxlength="16"
            autocomplete="off"
            v-model="operForm.passwd2"
            @keydown.enter.native="submitForm('operForm')"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('operForm')">提交</el-button>
        <el-button type="text" @click="toggleFullscreen()">
          <i class="el-icon-full-screen dialogFullscreen"></i>
        </el-button>
        <el-button type="text" @click="toggleDialog(false)">
          <i class="el-icon-circle-close dialogClose"></i>
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import "@/mock/Admin";
import Message from "@/plugins/message";
export default {
  name: "admin-edit",
  data() {
    let validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (value && value.length < 6 && value.length > 16) {
        callback(new Error("密码长度不能低于6位且不能大于16位"));
      } else {
        if (this.operForm.passwd2 !== "") {
          this.$refs.operForm.validateField("passwd2");
        }
        callback();
      }
    };
    let validatePassword2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入确认密码"));
      } else if (value && value.length < 6 && value.length > 16) {
        callback(new Error("确认密码长度不能低于6位且不能大于16位"));
      } else if (value !== this.operForm.passwd) {
        callback(new Error("确认密码和上面的密码不相同!"));
      } else {
        callback();
      }
    };
    return {
      dialogFormTitle: "修改密码",
      fullscreen: false,
      dialogFormVisible: false,
      operForm: {
        teaid: "",
        passwd: "",
        passwd2: ""
      },
      rules: {
        teaid: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        passwd: [{ validator: validatePassword, trigger: "blur" }],
        passwd2: [{ validator: validatePassword2, trigger: "blur" }]
      }
    };
  },
  created() {
    this.operForm.teaid = this.$store.state.teaid;
  },
  methods: {
    submitForm: function(formName) {
      const self = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          // http.send({ url: url, sendType: 'post', param: this.operForm, showSuccessTip: true }).then(data => {
          //   if (data.code === 0) {
          //     self.$refs[formName].resetFields()
          //     self.toggleDialog(false)
          //     self.$parent.loadMine()
          //   }
          // })
          axios.post("Admin/update", { adminInfo: this.operForm }).then(res => {
            const { code, msg, item } = res.data;
            // const teaid = item.teaid;
            if (code === 0) {
              Message.success(msg);
              self.$refs[formName].resetFields();
              self.toggleDialog(false);
              self.$parent.loadMine();
            } else {
              Message.error("编辑失败");
            }
          });
        }
      });
    },
    toggleDialog: function(flag) {
      this.dialogFormVisible = flag;
    },
    toggleFullscreen: function() {
      this.fullscreen = !this.fullscreen;
    }
  }
  // watch: {
  //   '$parent.dialogEditTime': function () {
  //     if (this.$parent.dialogId === 0) {
  //       if (this.dialogLastOperation === 'edit') {
  //         this.$nextTick(() => {
  //           this.$refs.operForm.resetFields()
  //         })
  //         this.dialogLastOperation = 'add'
  //       }
  //     } else {
  //       this.dialogLastOperation = 'edit'
  //       this.$nextTick(() => {
  //         this.$refs.operForm.resetFields()
  //       })
  //       http.send({ url: '/Admin/detail', param: { params: { id: this.$parent.dialogId } } }).then(data => {
  //         Object.assign(this.operForm, data.extra)
  //         this.operForm.passwd = ''
  //         this.operForm.passwd2 = ''
  //       })
  //     }
  //   }
  // }
};
</script>
